﻿using System.IO;
using IndicoInterface;
using DeepTalkDBServiceTest.DTDBService;
using DeepTalkDBServiceTest.ServiceReference1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using System;
using System.ServiceModel;

namespace DeepTalkDBServiceTest
{
    class Utils
    {
        /// <summary>
        /// Buidls the XML for an agenda of some random confID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetTalkXML(string id)
        {
            /// Submit a talk for rendering -- should not crash! :-)

            AgendaInfo info = new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?showSession=all&showDate=all&view=xml&fr=no&confId=" + id);
            StringWriter sr = new StringWriter();
            info.Seralize(sr);
            return sr.ToString();
        }

        /// <summary>
        /// Default return of a single XML for an agenda
        /// </summary>
        /// <returns></returns>
        public static string GetTalkXML()
        {
            return GetTalkXML("a042880");
        }

        /// <summary>
        /// Submit some number of talks for rendering...
        /// </summary>
        /// <param name="number"></param>
        public static void SubmitTalks(int number)
        {
            for (int i = 0; i < number; i++)
            {
                SubmitTalk("submit-" + i.ToString());
            }
        }

        /// <summary>
        /// Subit a single talk for rendering
        /// </summary>
        /// <param name="p"></param>
        private static void SubmitTalk(string confID)
        {
            using (DTDBClient rc = new DTDBClient())
            {
                string agendaXML = Utils.GetTalkXML(confID);
                rc.SubmitNewTalk(agendaXML, "Submitted Talk " + confID, DateTime.Now);
            }
        }

        /// <summary>
        /// Render some number of talks.
        /// </summary>
        /// <param name="number"></param>
        public static void RenderTalks(int number)
        {
            for (int i = 0; i < number; i++)
            {
                RenderTalk("aa121-" + i.ToString());
            }
        }

        /// <summary>
        /// Simple render-guy callback.
        /// </summary>
        class callbackGO : ServiceReference1.IDTRenderCallback
        {
            public DTRenderClient Render { get; set; }

            private AutoResetEvent alldoneevt = new AutoResetEvent(false);
            public void WaitForDone()
            {
                if (!alldoneevt.WaitOne(1000 * 20))
                    throw new System.Exception("Failed while wiating for all done event!");

            }

            public bool RenderTalk(DeepTalkDBServiceTest.ServiceReference1.DTRenderTalkInfo info)
            {
                ThreadPool.QueueUserWorkItem(arg =>
                {
                    Render.RenderDone(info.ID, (float)100.0, 1);
                    alldoneevt.Set();
                });
                return true;
            }
        }

        /// <summary>
        /// Render a single talk
        /// </summary>
        /// <param name="confID"></param>
        private static void RenderTalk(string confID)
        {
            using (DTDBClient rc = new DTDBClient())
            {
                string agendaXML = Utils.GetTalkXML(confID);
                rc.SubmitNewTalk(agendaXML, "Talk " + confID, DateTime.Now);
                int id = rc.FindTalk(agendaXML);

                callbackGO cb = new callbackGO();
                using (DTRenderClient render = new DTRenderClient(new InstanceContext(cb)))
                {
                    cb.Render = render;
                    render.ReadyForWork();
                    cb.WaitForDone();
                }
                Assert.IsTrue(rc.CanBeViewed(id), "Talk is rendered and should have been viewable!");
            }
        }
    }
}
